package BackTracking;

import java.util.ArrayList;
import java.util.List;

/**
 * Lettcode 17: https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/
 */
public class letterCombinations {
    private static final String[] KEYS = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if (digits == null || digits.length() == 0) return res;
        doCombination(new StringBuilder(), res, digits);
        return res;
    }

    private void doCombination(StringBuilder prefix, List<String> Combinations, final String digits) {
        if (prefix.length() == digits.length()) {
            Combinations.add(prefix.toString());
            return;
        }
        int curDigits = digits.charAt(prefix.length()) - '0';
        for (char c: KEYS[curDigits].toCharArray()) {
            prefix.append(c);
            doCombination(prefix, Combinations, digits);
            prefix.deleteCharAt(prefix.length() - 1);
        }
    }
}
